{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'c:\\\\Users\\\\user\\\\Drive\\\\s-hero\\\\Code'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os, shutil\n",
    "os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1536\n"
     ]
    }
   ],
   "source": [
    "origin_path = r'C:\\Users\\user\\Drive\\s-hero\\Code\\Data'\n",
    "file_list = os.listdir(origin_path)\n",
    "print(len(os.listdir(origin_path)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "767 767\n"
     ]
    }
   ],
   "source": [
    "from glob import glob \n",
    "image_list = sorted(glob(r'C:\\Users\\user\\Drive\\s-hero\\Code\\Data\\*.jpg'))\n",
    "annot_list = sorted(glob(r'C:\\Users\\user\\Drive\\s-hero\\Code\\Data\\*.txt'))\n",
    "print(len(image_list), len(annot_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "train_image_list, valid_image_list , train_annot_list, valid_annot_list = train_test_split(image_list, annot_list, test_size=0.25)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\\\B2_046.jpg'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_image_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "575 575 192 192\n"
     ]
    }
   ],
   "source": [
    "print(len(train_image_list), len(train_annot_list), len(valid_image_list), len(valid_annot_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'B1_003'"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_image_list[1][-10:-4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "image_names = []\n",
    "for items in train_image_list:\n",
    "    image_names.append(items[-10:-4])\n",
    "image_names.sort()\n",
    "\n",
    "annot_names = []\n",
    "for items in train_image_list:\n",
    "    annot_names.append(items[-10:-4])\n",
    "annot_names.sort()\n",
    "\n",
    "assert image_names == annot_names  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_image_move_path = r'C:\\Users\\user\\Drive\\s-hero\\Code\\Data\\train\\images'\n",
    "train_annot_move_path = r'C:\\Users\\user\\Drive\\s-hero\\Code\\Data\\train\\labels'\n",
    "valid_image_move_path = r'C:\\Users\\user\\Drive\\s-hero\\Code\\Data\\valid\\images'\n",
    "valid_annot_move_path = r'C:\\Users\\user\\Drive\\s-hero\\Code\\Data\\valid\\labels'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "for item in train_image_list:\n",
    "    shutil.move(item, train_image_move_path + item[-11:])\n",
    "\n",
    "for item in valid_image_list:\n",
    "    shutil.move(item, valid_image_move_path + item[-11:])\n",
    "\n",
    "for item in train_annot_list:\n",
    "    shutil.move(item, train_annot_move_path + item[-11:])\n",
    "    \n",
    "for item in valid_annot_list:\n",
    "    shutil.move(item, valid_annot_move_path + item[-11:])"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "790dd84243cf46e5e9b666dfb25c5c04395df9f262adebb0a07d3d946c1bc019"
  },
  "kernelspec": {
   "display_name": "Python 3.9.7 64-bit ('torch': conda)",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
